﻿using System.Collections.Generic;
using Newtonsoft.Json;
using OrkutAPILibrary.Constants;
using OrkutAPILibrary.Entities;

namespace OrkutAPILibrary.Methods
{
    /// <summary>
    /// Orkut Photo Factory
    /// </summary>
    static class OrkutPhotoFactory
    {
        /// <summary>
        /// Gets the self photos from album.
        /// </summary>
        /// <param name="albumId">The album id.</param>
        /// <param name="startIndex">The start index.</param>
        /// <param name="count">The count.</param>
        /// <returns></returns>
        internal static OrkutData<OrkutPhoto> GetSelfPhotosFromAlbum(string albumId, int startIndex = 0, int count = OrkutLibraryConstants.DefaultNoOfPictures)
        {
            var extraData = new Dictionary<string, string>
            {
				{ "method", "mediaitems.get" },
				{ "userId", "@me" },
				{ "groupId", "@self" },
				{ "albumId", albumId },
                { "count", count.ToString()},
                { "startIndex", startIndex.ToString()}
			};

            var requestResponse = OrkutLibrary.ExecuteRequest(extraData);

            return string.IsNullOrEmpty(requestResponse)
                ? null
                : JsonConvert.DeserializeObject<OrkutData<OrkutPhoto>>(requestResponse);
        }

        /// <summary>
        /// Gets the self photo from album.
        /// </summary>
        /// <param name="albumId">The album id.</param>
        /// <param name="photoId">The photo id.</param>
        /// <returns></returns>
        internal static OrkutData<OrkutPhoto> GetSelfPhotoFromAlbum(string albumId, string photoId)
        {
            var extraData = new Dictionary<string, string>
            {
				{ "method", "mediaitems.get" },
				{ "userId", "@me" },
				{ "groupId", "@self" },
				{ "albumId", albumId },
                { "mediaItemId", photoId},
			};

            var requestResponse = OrkutLibrary.ExecuteRequest(extraData);

            return string.IsNullOrEmpty(requestResponse)
                ? null
                : JsonConvert.DeserializeObject<OrkutData<OrkutPhoto>>(requestResponse);
        }

        /// <summary>
        /// Gets the photos from album.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <param name="albumId">The album id.</param>
        /// <param name="startIndex">The start index.</param>
        /// <param name="count">The count.</param>
        /// <returns></returns>
        internal static OrkutData<OrkutPhoto> GetPhotosFromAlbum(string userId, string albumId, int startIndex = 0, int count = OrkutLibraryConstants.DefaultNoOfPictures)
        {
            var extraData = new Dictionary<string, string>
            {
				{ "method", "mediaitems.get" },
				{ "userId", userId },
				{ "groupId", "@self" },
				{ "albumId", albumId },
                { "count", count.ToString()},
                { "startIndex", startIndex.ToString()}
			};

            var requestResponse = OrkutLibrary.ExecuteRequest(extraData);

            return string.IsNullOrEmpty(requestResponse)
                ? null
                : JsonConvert.DeserializeObject<OrkutData<OrkutPhoto>>(requestResponse);
        }

        /// <summary>
        /// Gets the photo from album.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <param name="albumId">The album id.</param>
        /// <param name="photoId">The photo id.</param>
        /// <returns></returns>
        internal static OrkutData<OrkutPhoto> GetPhotoFromAlbum(string userId, string albumId, string photoId)
        {
            var extraData = new Dictionary<string, string>
            {
				{ "method", "mediaitems.get" },
				{ "userId", userId },
				{ "groupId", "@self" },
				{ "albumId", albumId },
                { "mediaItemId", photoId},
			};

            var requestResponse = OrkutLibrary.ExecuteRequest(extraData);

            return string.IsNullOrEmpty(requestResponse)
                ? null
                : JsonConvert.DeserializeObject<OrkutData<OrkutPhoto>>(requestResponse);
        }

        /// <summary>
        /// Deletes the photo.
        /// </summary>
        /// <param name="albumId">The album id.</param>
        /// <param name="photoId">The photo id.</param>
        /// <returns></returns>
        internal static bool DeletePhoto(string albumId, string photoId)
        {
            var extraData = new Dictionary<string, string>
            {
				{ "method", "mediaitems.delete" },
				{ "userId", "@me" },
				{ "groupId", "@self" },
				{ "albumId", albumId },
                { "mediaItemId", photoId},
			};

            var requestResponse = OrkutLibrary.ExecuteRequest(extraData);

            return string.IsNullOrEmpty(requestResponse) ? true : false;
        }

        /// <summary>
        /// Uploads the photo.
        /// </summary>
        /// <param name="albumId">The album id.</param>
        /// <param name="imageBytes">The image bytes.</param>
        /// <param name="photoType">Type of the photo.</param>
        /// <param name="photoTitle">The photo title.</param>
        /// <returns></returns>
        internal static OrkutData<OrkutPhoto> UploadPhoto(string albumId, byte[] imageBytes, string photoType, string photoTitle)
        {
            throw new OrkutException("This method is currently not supported by Orkut. This is just a placeholder.");
        }

        /// <summary>
        /// Updates the photo.
        /// </summary>
        /// <param name="photoToUpdate">The photo to update.</param>
        /// <returns></returns>
        internal static bool UpdatePhoto(OrkutPhoto photoToUpdate)
        {
            var extraData = new Dictionary<string, string>
            {
				{ "method", "mediaitems.update" },
				{ "userId", "@me" },
				{ "groupId", "@self" },
				{ "albumId", photoToUpdate.AlbumId },
                { "mediaItemId", photoToUpdate.Id},
                { "mediaItem", JsonConvert.SerializeObject(photoToUpdate)},
			};

            var requestResponse = OrkutLibrary.ExecuteRequest(extraData);

            return string.IsNullOrEmpty(requestResponse) ? true : false;
        }
    }
}
